﻿using System;
using System.Linq.Expressions;
using SBACN.WebSite.Cache;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;

namespace SBACN.WebSite.Admin.AboutGuild
{

	public partial class MemberList : AdminBasePage
    {
		/// <summary>
		/// 系统字典类型
		/// </summary>
		public string SystemType
		{
			get
			{
				//if (ViewState["SystemType"] == null)
				//    return "00010005";//默认值
				//else
				//    return ViewState["SystemType"].ToString();
				return this.SystemCode;
			}
			private set
			{
				ViewState["SystemType"] = this.SystemCode;
			}
		}

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowMemberList(1);
            }
        }

        protected void ShowMemberList(int pageIndex)
        {

            Expression<Func<TB_Member, bool>> f = (p =>true);
            Expression<Func<TB_Member, string>> order = (p => p.Name);
            int records = 0;
            this.MemeberList.DataSource = DAL.DALHelper.Instance.GetList<TB_Member, string>(f, order, true, this.Pager1.PageSize, pageIndex, out records);
            DataBind();
            this.Pager1.RecordCount = records;
            this.Pager1.InitControl();
        }

        public void BindSelectedPageIndexChanged(int pageIndex, int nextPage)
        {
            ShowMemberList(nextPage);
        }

        public string MemberClassName(string Code)
        {
            var resutl = SystemMapCache.Instance[Code];
            return resutl.Name;
        }

		protected void Del_Click(object sender, EventArgs e)
		{
			int ID = 0;
			if (!int.TryParse(this.Hid_ID.Value, out ID))
			{//	无效ID
				ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script >alert('删除失败，请刷新后重新操作！');location=location;</script>");
				return;
			}

			Expression<Func<View_CinfoRelationMember, bool>> F = (p => p.MID == ID);
			var resultV = DAL.DALHelper.Instance.GetList<View_CinfoRelationMember>(F);
			if (resultV.Count > 0)
			{// 存在关系
				string CInfoN ="";
				foreach( var l in resultV)
				{
					CInfoN = CInfoN+l.CName+"\\n";
				}
				ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script >alert('删除失败！\\n此会员单位仍属于:\\n" + CInfoN + "\\n 删除会员单位请先解除与相关理事会的所属关系。');location=location;</script>");
				return;
			}

			// 删除
			var resultM = DAL.DALHelper.Instance.GetObject<TB_Member>("ID", ID);
			DAL.DALHelper.Instance.Delete<TB_Member>(resultM);
			DAL.DALHelper.Instance.SaveChanges();
			Response.Write("<script>alert('删除成功！');location=location;</script>");
			Response.End();

		}

		protected void btn_NewsRefresh_Click(object sender, EventArgs e)
		{
			ShowMemberList(this.Pager1.CurrentPageIndex);
		}


    }
}
